<template>
  <view>
   <view class="goodsList">
    <view v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item.goods_id)">
      <!-- 商品信息组件 -->
     <myGoods :item="item"></myGoods>
    </view>
   </view>
  </view>
</template>

<script>
  import myGoods from '@/components/my-goods/my-goods.vue'
  export default {
  components:{myGoods},
    data() {
      return {
        // 请求参数对象
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        goodsList: [],     // 商品列表的数据
        total: 0,         // 总数量，用来实现分页
        isLoading:false   //节流阀，判断是否正在请求数据
       
      }
      },
      onLoad(options) {
          this.queryObj.query = options.query || ''
          this.queryObj.cid = options.cid || ''
          this.getGoodsList()
        },
        methods: {
         async getGoodsList(){
           this.isLoading=true   //打开节流阀
           const {message}=await this.$u.get("/goods/search",this.queryObj)
           console.log(message)
           this.isLoading=false   //关闭节流阀
           this.goodsList=[...this.goodsList,...message.goods]
           this.total=message.total
         },
         //跳转到商品详情页面
          gotoDetail(goods_id){
            uni.navigateTo({
              url:"/subpkg/goods_detail/goods_detail?goods_id="+goods_id
            })
          }
        },
        //上拉加载
        onReachBottom() {
          if(this.queryObj.pagenum!==Math.ceil(this.total/this.queryObj.pagesize)){
            if(this.isLoading) return
            this.queryObj.pagenum+=1
            this.getGoodsList()
          }else{
            uni.showToast({
              title:'我是有底线的',
              icon:'none'
            })
          }
        },
        // 下拉刷新
        async onPullDownRefresh() {
          this.queryObj.pagenum=1
          this.total=0
          this.goodsList=[]
          //async函数本身就会返回一个promise可以用await进行等待
          await this.getGoodsList()
          //关闭下拉刷新效果
          uni.stopPullDownRefresh()
        },
        
    }
</script>

<style lang="scss" scoped>

</style>
